import * as multer from 'multer'
import * as fs from 'fs'
import * as path from 'path'

export const multerStorage = multer.diskStorage({
  destination: function (req, file, cb) {
    try {
      fs.mkdirSync(path.join(process.cwd(), 'uploads'))
    } catch (e) {}

    cb(null, path.join(process.cwd(), 'uploads'))
  },
  filename: function (req, file, cb) {
    const uniqueSuffix =
      Date.now() +
      '-' +
      Math.round(Math.random() * 1e9) +
      '.' +
      // 获取文件的后缀名
      path.extname(file.originalname).slice(1)
    cb(null, file.fieldname + '-' + uniqueSuffix)
  }
})
